package com.xt.base.util;

import java.io.File;
import java.io.FileInputStream;

public class HtmlTool {
  public HtmlTool() {
  }

  public static String HTMLEncode(String str) {
    String s = str;
    s = s.replaceAll(">", "&gt;");
    s = s.replaceAll("<", "&lt;");
    s = s.replaceAll(" ", "&nbsp;&nbsp;");
    s = s.replaceAll("\n", "<br>");
    return s;
  }
  public static String sendRenFormat(String str){
    String s = str;
    s = s.replaceAll("\n", "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    return s;
  }
  
  public static String filterHtmlTag(String input, int length) {
		if (input == null || input.trim().equals("")) {
			return "";
		}
		// 去掉所有html元素,
		String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
				"<[^>]*>", "");
		str = str.replaceAll("[(/>)<]", "");
		int len = str.length();
		if (len <= length) {
			return str;
		} else {
			str = str.substring(0, length);
			str += "";
		}
		return str;
	}
	
	public static void main(String[] args) throws Exception{
		File file = new File("d:/text.html");
		FileInputStream fis = new FileInputStream(file);
		byte[] bytes = new byte[60000];
		fis.read(bytes);
		String s = new String(bytes,"GBK").trim();
		System.out.println("123-----"+s);
		s = filterHtmlTag(s,s.length());
		System.out.println("abc-----"+s);
	}
}
